
#显示等待
from selenium.webdriver.support.ui import WebDriverWait
from datetime import datetime
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
class Base():
    #封装初始化浏览器
    def __init__(self,driver):
        self.driver=driver


    #封装URL
    def open_url(self,url):
        self.driver.get(url)


    #封装定位方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)

        except Exception as e:
            #报错截图
            takeAScreenshot=datetime.now().strftime('%Y%m%d %H%M%S')
            self.driver.save_screenshot(f'err_{takeAScreenshot}.png')
            print('未找到定位元素',e)

    #封装输入框事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #封装点击事件事件
    def click(self,loc):
        self.find_element(loc).click()
    #封装弹出事件
    def select(self,loc,x):
        dya1=Select(self.find_element(loc))
        dya1.select_by_value(x)

    def select1(self,loc,x):
        dya1 = Select(self.find_element(loc))
        dya1.select_by_index(x)

    def select2(self,loc,x):
        dya1 = Select(self.find_element(loc))
        dya1.select_by_visible_text(x)
    #封装鼠标事件
    def ActionChains(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #窗口切换
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #封装键盘事件
    def key(self,loc,x):
        self.find_element(loc).send_keys(Keys.CONTROL,x)

    #下滑
    def execute_script(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
